package com.lz.sports.ui

import android.os.Bundle
import android.view.View
import androidx.lifecycle.lifecycleScope
import com.lz.comm.BR
import com.lz.comm.SP
import com.lz.library.base.BaseActivity
import com.lz.library.dialog.BaseDialogFragment
import com.lz.library.extension.click
import com.lz.library.extension.resString
import com.lz.sports.LogUtils
import com.lz.sports.R
import com.lz.sports.adapter.ExamAdapter
import com.lz.sports.core.K_RULE_EXAM_TRIP
import com.lz.sports.core.v_ruleExamTrip
import com.lz.sports.databinding.ActExamBinding
import com.lz.sports.dialog.DelayedLoadingDialog
import com.lz.sports.dialog.DowntimeDialog
import com.lz.sports.dialog.ParcelableSelectDialog
import com.lz.sports.dialog.WarningDialog
import com.lz.sports.vm.ExamViewModel
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

//考试
class ExamActivity : BaseActivity<ExamViewModel, ActExamBinding>(),
    WarningDialog.OnWarningConfirmListener, ParcelableSelectDialog.OnSelectedListener,
    BaseDialogFragment.OnDismissListener {

    companion object {
        private const val REQUEST_FINISH = 1
        private const val REQUEST_RETEST = 2
        private const val REQUEST_START = 3
        private const val REQUEST_RESET = 4
    }

    private val adapter by lazy { ExamAdapter(SP.getBoolean(K_RULE_EXAM_TRIP, v_ruleExamTrip)) }

    override fun getLayoutRes(): Int = R.layout.act_exam

    override fun getVMVariableId(): Int = BR.vm

    override fun initObserve() {
        viewModel.refreshAdapter.observe(this) { refresh ->
            if (refresh) viewModel.currentGroup?.inspectList?.also { adapter.setNewData(it) }
        }
    }

    override fun initData(savedState: Bundle?) {
        binding.rvList.adapter = adapter

        lifecycleScope.launch {
            arrayOf(
                binding.ivBack,
                binding.tvPrepare,
                binding.tvConfirm,
                binding.tvGroup,
                binding.tvRetest,
                binding.tvPre,
                binding.tvStart,
                binding.tvNext,
                binding.tvSave,
                binding.tvPrint,
                binding.tvReset
            ).click(1000).collect { onClick(it) }
        }
    }

    private fun onClick(v: View) {
        when (v.id) {
            R.id.iv_back -> {
                if (!viewModel.canBack()) {
                    WarningDialog.show(this, R.string.exam_quit_w.resString(), requestCode = REQUEST_FINISH)
                } else {
                    finish()
                }
            }
            R.id.tv_prepare -> viewModel.playSound(2)
            R.id.tv_confirm -> viewModel.playSound(3)
            R.id.tv_group -> {
                ParcelableSelectDialog.show(
                    this,
                    R.string.exam_group_select.resString(),
                    viewModel.currentGroup,
                    viewModel.inspectGroupList
                )
            }
            R.id.tv_retest -> WarningDialog.show(
                this,
                R.string.exam_retest_w.resString(),
                requestCode = REQUEST_RETEST
            )
            R.id.tv_pre -> viewModel.pre()
            R.id.tv_start -> {
                if (!viewModel.isAllDeviceConnected()) {
                    WarningDialog.show(
                        this,
                        resources.getString(R.string.exam_start_w),
                        requestCode = REQUEST_START
                    )
                } else {
                    viewModel.preStart()
                    DowntimeDialog.show(this, ExamViewModel.BEGIN_TIME)
                }
            }
            R.id.tv_next -> viewModel.next()
            R.id.tv_save -> viewModel.save()
            R.id.tv_print -> viewModel.printer()
            R.id.tv_reset -> WarningDialog.show(
                this,
                resources.getString(R.string.exam_reset_w),
                requestCode = REQUEST_RESET
            )
        }
    }

    @Deprecated("Deprecated in Java")
    @Suppress("DEPRECATION")
    override fun onBackPressed() {
        if (!viewModel.canBack()) {
            WarningDialog.show(this, R.string.exam_quit_w.resString(), requestCode = REQUEST_FINISH)
            return
        }
        super.onBackPressed()
    }

    override fun onConfirm(dialog: WarningDialog, requestCode: Int) {
        dialog.dismiss()
        when (requestCode) {
            REQUEST_FINISH -> {
                finish()
            }
            REQUEST_RETEST -> {
                viewModel.restartPre()
                DowntimeDialog.show(this, ExamViewModel.BEGIN_TIME)
            }
            REQUEST_START -> {
                viewModel.preStart()
                DowntimeDialog.show(this, ExamViewModel.BEGIN_TIME)
            }
            REQUEST_RESET -> {
                viewModel.reset()
            }
        }
    }

    override fun onDismiss(dialogFragment: BaseDialogFragment<*>) {
        if (dialogFragment is DowntimeDialog) {
            viewModel.start()
        } else if (dialogFragment is DelayedLoadingDialog) {
            LogUtils.i("delayed loading .........")
        }
    }

    override fun onSelected(dialog: ParcelableSelectDialog, position: Int, requestCode: Int) {
        dialog.dismiss()
        viewModel.selectInspectGroup(position)
    }
}